/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Deal.PersonalInformation.Logic;

import Data.DAO.ICustomerStore;
import data.DAO.ICollectionStore;
import data.pojo.Collection;
import data.pojo.Customer;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;


/**
 *
 * @author 伯乐
 */
public class CollectionLogic {

    private ICustomerStore customerDS = new ICustomerStore();
    private ICollectionStore collectionDS = new ICollectionStore();

    //产生收藏号
    private String creatSerialNumber(Customer c){
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        int n = c.getCollections().size();
        return sdf.format(date)+n;
    }
    //"ISBN", "标题", "作者", "价格", "出版日期", "出版商",
    public boolean addToCollection(String user_id, String[] bookArr) {
        if(exist(user_id, bookArr)){ 
            return true; 
        }
        List<Customer> cuList = (List<Customer>)customerDS.getObjects();
        List<Collection> coList = (List<Collection>)collectionDS.getObjects();
        
        for(Customer c : cuList){
            if(c.getId().equals(user_id)){
                Collection coll = new Collection();
                coll.setIsbn(bookArr[0]);
                coll.setTitle(bookArr[1]);
                coll.setAuthor(bookArr[2]);
                coll.setPublisher(bookArr[4]);
                coll.setCustomer(user_id);
                coll.setSerialNumber(creatSerialNumber(c)); //产生收藏序列号，序列号=已有收藏数+1
                c.addCollection(coll); //添加到customer.txt文件里
                customerDS.update(cuList);
                
                coList.add(coll); //添加到collection.txt文件里
                collectionDS.update(coList);
                return true;
            }
        }
        return false;
    }
    
    //"ISBN", "标题", "作者", "价格", "出版日期", "出版商",
    private boolean exist(String user_id, String[] bookArr){
        List<Customer> cuList = (List<Customer>)customerDS.getObjects();
        for(Customer c : cuList){
            if(c.getId().equals(user_id)){
                List<Collection> cols = c.getCollections();
                for(Collection col:cols){
                    boolean v_isbn = col.getIsbn().equals(bookArr[0]);
                    boolean v_title = col.getTitle().equals(bookArr[1]);
                    boolean v_author = col.getAuthor().equals(bookArr[2]);
                    boolean v_publisher = col.getPublisher().equals(bookArr[4]);
                    if(v_isbn && v_title && v_author && v_publisher){
                        return true;
                    }
                }
                return false;
            }
        }
        return false;
    }

    //注意bookArr中的数据类型，比如书名、作者等要和次方法中的一致
    //bookArr:"ISBN", "标题", "作者", "出版日期", "出版商", "收藏日期", "收藏人"
    public boolean removeCollection(String user_id, String collectionID) {
        List<Customer> cuList = customerDS.getObjects();
        for(Customer c : cuList){    //从customer.txt文件里删除
            if(c.getId().equals(user_id)){ 
                List<Collection> collections = c.getCollections();
                for(int i=0; i<collections.size(); i++){
                    if(collections.get(i).getSerialNumber().equals(collectionID)){
                        collections.remove(i); 
                        customerDS.update(cuList); 
                        return removeFromCollectionTxt(user_id, collectionID); //从collection.txt文件里删除
                    }//end if
                } //end for
            }//end if
        }//end for 
        
        return false;
    }
    private boolean removeFromCollectionTxt(String user_id, String collectionID){
        List<Collection> coList = (List<Collection>)collectionDS.getObjects();
        for(Collection c : coList){
            if(c.getSerialNumber().equals(collectionID)){
                coList.remove(c);
                collectionDS.update(coList);
                return true;
            }
        }
        return false;
    }
    
    public Vector<List> CollectionTableVector(String user_id) {
        List<Customer> arr = customerDS.searchObjectsByAttr("id", user_id);
        if(arr.size()>0){
            Vector<Vector<String>> aVector = toCollectionTable(arr);
            if(aVector!=null)
                return new Vector<List>(aVector);
        }
        return null;
    }

    private Vector<Vector<String>> toCollectionTable(List<Customer> list) {
        try {
            List<Collection> coList = list.get(0).getCollections();
            if(coList.size()<=0){
                return null;
            }
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (int i = 0; coList!=null && i < coList.size(); i++) {
                reList.add((Vector<String>) toCollectionStringArray(coList.get(i)));
            }
            return reList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private List toCollectionStringArray(Collection collection) {
        Vector<String> list = new Vector<String>();
        list.add(collection.getSerialNumber());
        list.add(collection.getIsbn());
        list.add(collection.getTitle());
        list.add(collection.getAuthor());
        list.add(collection.getPublisher());
        list.add(collection.getCustomer());
        return list;
    }
}
